"""
URL configuration for ship_schedule project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.shortcuts import render
from django.http import HttpResponse
from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from .views import AntiCrawlerManagementView, anti_crawler_status
import os


@api_view(['GET'])
@permission_classes([AllowAny])
def api_health_check(request):
    """
    API健康检查端点
    用于验证API服务是否正常运行
    """
    return Response({
        'status': 'ok',
        'message': '船舶调度系统API服务正常运行',
        'version': '1.0.0'
    }, status=status.HTTP_200_OK)


def serve_frontend_spa(request, path=''):
    """
    为前端SPA路由服务index.html
    支持 /admin/*, /management/*, /dashboard, /schedule 等前端路由
    """
    # 前端构建文件路径
    frontend_build_path = os.path.join(settings.BASE_DIR.parent, 'ship-schedule-management-ui', 'dist')
    index_path = os.path.join(frontend_build_path, 'index.html')
    
    try:
        with open(index_path, 'r', encoding='utf-8') as f:
            content = f.read()
        return HttpResponse(content, content_type='text/html')
    except FileNotFoundError:
        return HttpResponse('Frontend build not found', status=404)


urlpatterns = [
    # 前端管理页面路由 (必须在 Django admin/ 之前，避免被admin/覆盖)
    path('admin/users/', serve_frontend_spa, name='spa_admin_users'),
    path('admin/roles/', serve_frontend_spa, name='spa_admin_roles'), 
    path('admin/permissions/', serve_frontend_spa, name='spa_admin_permissions'),
    path('admin/local-fees/', serve_frontend_spa, name='spa_admin_local_fees'),
    
    # Django 管理后台 (在前端admin路由之后)
    path('admin/', admin.site.urls),
    
    # 其他前端SPA路由
    path('management/', serve_frontend_spa, name='spa_management'),
    path('management/<path:path>/', serve_frontend_spa, name='spa_management_sub'),
    path('dashboard/', serve_frontend_spa, name='spa_dashboard'),
    path('schedule/', serve_frontend_spa, name='spa_schedule'),
    path('profile/', serve_frontend_spa, name='spa_profile'),

    # API根路径健康检查
    path('api/', api_health_check, name='api_health_check'),

    # 反爬虫管理API
    path('api/anti-crawler/status/', anti_crawler_status, name='anti_crawler_status'),
    path('api/anti-crawler/manage/', AntiCrawlerManagementView.as_view(), name='anti_crawler_manage'),

    # 认证相关API
    path('api/auth/', include('authentication.urls')),

    # 船舶航线相关API
    path('api/', include('schedules.urls')),

    # 本地费用相关API
    path('api/local-fees/', include('local_fees.urls')),
    
    # 系统消息相关API
    path('api/', include('system_messages.urls')),
]

# 开发环境下提供媒体文件服务
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# 前端静态文件服务
frontend_build_path = os.path.join(settings.BASE_DIR.parent, 'ship-schedule-management-ui', 'dist')
urlpatterns += static('/assets/', document_root=os.path.join(frontend_build_path, 'assets'))
urlpatterns += static('/ship-icon.svg', document_root=frontend_build_path)
